$(function () {
    // 修改邮箱
    $('#email-dialog-modify').on('click', function (e) {
        e.preventDefault();

        var email = $('#email-dialog-email').val();
        if (!email) {
            eui.msg('请输入新邮箱');
            return;
        }

        $.ajax({
            url: '/user/emailChange',
            data: {
                email: email,
            },
            type: 'post',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    eui.alert(obj.msg);
                } else {
                    eui.msg('修改失败，' + obj.msg);
                }
            },
            error: function () {
                eui.alert('系统出错');
            }
        });
    });

    // 修改密码
    $('#pwd-dialog-modify').on('click', function (e) {
        e.preventDefault();

        var oldPwd = $('#pwd-dialog-old-pwd').val();
        if (!oldPwd) {
            eui.msg('请输入旧密码');
            return;
        }

        var newPwd = $('#pwd-dialog-new-pwd').val();
        if (!newPwd) {
            eui.msg('请输入新密码');
            return;
        }

        var repeatPwd = $('#pwd-dialog-repeat-pwd').val();
        if (!repeatPwd) {
            eui.msg('请输入重复密码');
            return;
        }

        if (newPwd != repeatPwd) {
            eui.msg('两次密码输入不一致');
            return;
        }

        $.ajax({
            url: '/user/changePswd',
            data: {
                oldPswd: oldPwd,
                password: newPwd
            },
            type: 'post',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    eui.msg('修改成功');
                    $.ajax({
                        url: '/user/logout',
                        //type: 'POST',
                        dataType: 'json',
                        success: function (obj) {
                            if (obj.code === 0) {
                                window.location.reload(true);
                            } else {
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            eui.alert('系统出错');
                        }
                    });
                } else {
                    eui.msg('修改失败，' + obj.msg);
                }
            },
            error: function () {
                eui.alert('系统出错');
            }
        });

    });

    $('#modify-pwd').on('click', function (e) {
        e.preventDefault();

        $('#pwd-wrap').dialog();
    });

    $('#modify-email').on('click', function (e) {
        e.preventDefault();

        $('#email-wrap').dialog();
    });

    $('#edit-honor').on('click', function (e) {
        e.preventDefault();
        $('#honor-info-box').hide();
        $('#honor-edit-box').show();
        $(this).hide();
        $('#honor-save').show();
    });

    // 添加荣誉
    $('#add-honor').on('click', function (e) {
        e.preventDefault();

        $('#honor-wrap').dialog();
    });

    // 添加荣誉完成
    $('#honor-dialog-btn').on('click', function (e) {
        e.preventDefault();

        var honor = $('#honor-dialog-input').val();
        if (!honor) {
            eui.msg('请输入内容');
            return;
        }

        if (page.honor) {
            page.honor += ',' + honor;
        } else {
            page.honor = honor;
        }
        $.ajax({
            url: '/user/update',
            data: {
                honor: page.honor,
            },
            type: 'POST',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    eui.msg('添加成功');
                    setTimeout(function () {
                        location.reload(true);
                    }, 800);

                    $('#honor-wrap').dialog();

                } else {
                    eui.msg('添加失败');
                }

            },
            error: function () {
                eui.alert('系统出错');
            }
        })


    });

    // 保存荣誉
    $('#honor-save').on('click', function (e) {
        e.preventDefault();
        var honorArr = [];
        $('#honor-list').find('input').each(function () {
            var honor = $(this).val();
            if (honor) {
                honorArr.push(honor);
            }
        });

        $.ajax({
            url: '/user/update',
            data: {
                honor: honorArr.join(','),
            },
            type: 'POST',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    eui.msg('修改成功');
                    setTimeout(function () {
                        location.reload(true);
                    }, 1000);
                } else {
                    eui.msg('修改失败');
                }

            },
            error: function () {
                eui.alert('系统出错');
            }
        })
    });

    // 修改个人信息
    $('#edit1').on('click', function (e) {
        e.preventDefault();
        $(this).hide();
        $('#info1').hide();
        $('#edit-box-1').show();
        $('#save1').show();
    });
    $('#edit2').on('click', function (e) {
        e.preventDefault();
        $(this).hide();
        $('#info2').hide();
        $('#edit-box-2').show();
        $('#save2').show();
    });

    $('#save1').on('click', function (e) {
        e.preventDefault();

        var name = $('[name="name"]').val();
        if (!name) {
            eui.msg('请输入姓名');
            return;
        }
        var area = $('[name="area"]').val();
        var phone = $('[name="phone"]').val();
        /* if (!phone) {
         eui.msg('请输入电话');
         return;
         }*/

        var sex = $('[name="sex"]').val();

        var formData = new FormData();

        $.ajax({
            url: '/user/update',
            data: {
                name: name,
                sex: sex,
                area: area,
                phone: phone
            },
            type: 'POST',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    eui.msg('修改成功');
                    setTimeout(function () {
                        location.reload(true);
                    }, 1000);
                } else {
                    eui.msg('修改失败');
                }

            },
            error: function () {
                eui.alert('系统出错');
            }
        })
    });

    $('#save2').on('click', function (e) {
        e.preventDefault();

        var education = $('[name="education"]').val();
        var professional = $('[name="professional"]').val();
        var school = $('[name="school"]').val();
        var desc = $('[name="desc"]').val();

        var formData = new FormData();

        $.ajax({
            url: '/user/update',
            data: {
                education: education,
                professional: professional,
                school: school,
                desc: desc,
            },
            type: 'POST',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    eui.msg('修改成功');
                    setTimeout(function () {
                        location.reload(true);
                    }, 1000);
                } else {
                    eui.msg('修改失败');
                }

            },
            error: function () {
                eui.alert('系统出错');
            }
        })
    });

    $('#school').on('click', function () {
        $('#school-wrap').dialog();
    });

    // 设置是否对外显示重要信息
    $('#modify-open').on('click', function (e) {
        e.preventDefault();

        $.ajax({
            url: '/user/hideInfoToggle',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    //location.reload(true);
                    if (page.isOpen === 1) {
                        page.isOpen = 0;
                        $('#open-state').text('否');
                    } else {
                        page.isOpen = 1;
                        $('#open-state').text('是');
                    }
                } else {
                    eui.msg('操作失败');
                }
            },
            error: function () {
                eui.msg('系统出错');
            }
        });

    });

    // 上传头像
    $('#upload-avatar').on('click', function (e) {
        e.preventDefault();

        $('#avatar-wrap').dialog();
        //$('#avatar-input').trigger('click');
    });

    $('#select-image').on('click', function (e) {
        e.preventDefault();

        $('#avatar-input').trigger('click');
    });

    // 初始化头像剪裁框
    $('#avatar-image').cropper({
        preview: '.avatar-modify-preview',
        aspectRatio: 1 / 1,
        minContainerWidth: 300,
        minContainerHeight: 300
    });

    /*$('#avatar-input').on('change', function () {
        if ($(this).val()) {
            var formData = new FormData();
            var fileName = $(this).val();
            var file = $('#avatar-input')[0].files[0];
            var dot = fileName.lastIndexOf('.');
            var type = fileName.toLowerCase().substring(dot + 1);
            if (type !== 'jpg' && type !== 'png' ) {
                eui.msg('图片格式不正确');
                return;
            }

            formData.append('file', file);

            $.ajax({
                url: '/user/update',
                data: formData,
                type: 'POST',
                dataType: 'json',
                processData: false,
                contentType: false,
                success: function (obj) {
                    if (obj.code === 0) {
                        eui.msg('修改成功');
                        setTimeout(function () {
                            location.reload(true);
                        }, 1000);
                    } else {
                        eui.msg('修改失败');
                    }

                },
                error: function () {
                    eui.alert('系统出错');
                }
            })
        }
    });*/

    $('#avatar-modify-finish').on('click', function () {
        console.log(1);
        $('#avatar-image').cropper('getCroppedCanvas').toBlob(function (blob) {
            var formData = new FormData();
            formData.append('file', blob);

            $.ajax({
                url: '/user/update',
                data: formData,
                type: 'POST',
                dataType: 'json',
                processData: false,
                contentType: false,
                success: function (obj) {
                    if (obj.code === 0) {
                        eui.msg('修改成功');
                        setTimeout(function () {
                            location.reload(true);
                        }, 1000);
                    } else {
                        eui.msg('修改失败');
                    }

                },
                error: function () {
                    eui.alert('系统出错');
                }
            })
        });
    });

    showSchool();
    function showSchool(input) {
        $.ajax({
            url: '/common/schoolSearch',
            data: {
                key: input,
            },
            dataType: 'json',
            success: function (obj) {
                var list = obj.list;
                if (!list.length) {
                    $('#school-selector-list').hide();
                    $('#school-selector-empty').show();
                    return;
                }
                $('#school-selector-empty').hide();
                var $list = $('#school-selector-list');
                $list.show();
                $list.empty();
                var html = '';

                for (var i = 0; i < list.length; i++) {
                    //console.log(list[i].name);
                    html += '<li><a href="#">' + list[i].name + '</a></li>'
                }
                $list.html(html);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                eui.alert('系统出错');
            }
        });
    }

    $('#school-selector-search').on('input', function () {
        var val = $(this).val();
        showSchool(val);
    });

    $('#school-selector-list').on('click', 'li a', function (e) {
        e.preventDefault();
        $('#school').val($(this).text());

        $('#school-wrap').dialog('hide');
    }); 
});